﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsTest
{
    /// <summary>
    /// 投影类
    /// </summary>
    class Projection
    {
        /// <summary>
        /// 区域纵向向上投影
        /// </summary>
        public static void VerticalUpProjection(byte[,] im, ref byte[,] gs, int rw, int rh, int startY)
        {
            for (int i = 0; i < rw; i++)
            {
                var s = 0;
                for (int j = startY; j < startY + rh; j++)
                {
                    s += im[i, j];
                }

                for (int j = startY; j < startY + s; j++)
                {
                    gs[i, j] = 1;
                }
            }
        }

        /// <summary>
        /// 横向投影（Y轴投影）
        /// </summary>
        public static byte[,] HorizontalProjection(byte[,] im)
        {
            var w = im.GetLength(0);
            var h = im.GetLength(1);
            var gs = new byte[w, h];

            for (int j = 0; j < h; j++)
            {
                var s = 0;
                for (int i = 0; i < w; i++)
                {
                    s += im[i, j];
                }

                for (int i = 0; i < s; i++)
                {
                    gs[i, j] = 1;
                }
            }

            return gs;
        }

        /// <summary>
        /// 纵向投影（X轴投影）
        /// </summary>
        public static byte[,] VerticalProjection(byte[,] im)
        {
            var w = im.GetLength(0);
            var h = im.GetLength(1);
            var gs = new byte[w, h];

            for (int i = 0; i < w; i++)
            {
                var s = 0;
                for (int j = 0; j < h; j++)
                {
                    s += im[i, j];
                }

                for (int j = h - s; j < h; j++)
                {
                    gs[i, j] = 1;
                }
            }

            return gs;
        }

        /// <summary>
        /// 横向形心投影（Y轴投影）
        /// </summary>
        public static byte[,] HorizontalCenterProjection(byte[,] im, int x)
        {
            var w = im.GetLength(0);
            var h = im.GetLength(1);
            var gs = new byte[w, h];

            for (int j = 0; j < h; j++)
            {
                var s1 = 0;
                for (int i = 0; i < x; i++)
                {
                    s1 += im[i, j];
                }

                for (int i = x - s1; i < x; i++)
                {
                    gs[i, j] = 1;
                }

                var s2 = 0;
                for (int i = x; i < w; i++)
                {
                    s2 += im[i, j];
                }

                for (int i = x; i < x + s2; i++)
                {
                    gs[i, j] = 1;
                }
            }

            return gs;
        }
    }
}
